package glen.util;

import javax.faces.FacesException;
import javax.faces.application.ViewHandler;
import javax.faces.application.ViewHandlerWrapper;
import javax.faces.component.UIComponent;
import javax.faces.component.UIViewRoot;
import javax.faces.component.visit.VisitCallback;
import javax.faces.component.visit.VisitContext;
import javax.faces.component.visit.VisitResult;
import javax.faces.context.FacesContext;
import java.io.IOException;

/**
 * Created by rachatanawee on 5/2/2014.
 */
public class MyViewHandler extends ViewHandlerWrapper {
    private ViewHandler defaultViewHandler = null;

    public MyViewHandler() {
    }

    public MyViewHandler(ViewHandler defaultViewHandler) {
        super();
        this.defaultViewHandler = defaultViewHandler;
    }

    @Override
    public void renderView(FacesContext context, UIViewRoot viewToRender) throws IOException, FacesException {
        viewToRender.visitTree(VisitContext.createVisitContext(context),
                new VisitCallback() {

                    @Override
                    public VisitResult visit(VisitContext context, UIComponent target) {

                        // Authorization logic here
                        //    target.setRendered(false);
                        return VisitResult.ACCEPT;
                    }
                }
        );

        defaultViewHandler.renderView(context, viewToRender);
    }

    @Override
    public ViewHandler getWrapped() {
        return defaultViewHandler;
    }
}
